# from typing import Text
import requests
from lxml import etree
import webbrowser
from concurrent.futures import ThreadPoolExecutor
from requests import get, head
import time,re
# from selenium import webdriver
def geturl(website,path):
    # 获取源码
    html = requests.get(website)
    html.encoding='gb2312'

    # 打印源码
    etree_html = etree.HTML(html.text)
    link = etree_html.xpath('{}/li/a/@href'.format(path))
    return link

    

class downloader:
    def __init__(self, url, num, name):
        self.url = url
        self.num = num
        self.name = name
        self.getsize = 0
        r = head(self.url, allow_redirects=True)
        self.size = int(r.headers['Content-Length'])
        self.flagDone = False

    def down(self, start, end, chunk_size=10240):
        headers = {'range': f'bytes={start}-{end}'}
        r = get(self.url, headers=headers, stream=True)
        with open(self.name, "rb+") as f:
            f.seek(start)
            for chunk in r.iter_content(chunk_size):
                f.write(chunk)
                self.getsize += chunk_size

    def main(self):
        start_time = time.time()
        f = open(self.name, 'wb')
        f.truncate(self.size)
        f.close()
        tp = ThreadPoolExecutor(max_workers=self.num)
        futures = []
        start = 0
        for i in range(self.num):
            end = int((i+1)/self.num*self.size)
            future = tp.submit(self.down, start, end)
            futures.append(future)
            start = end+1
        while True:
            process = self.getsize/self.size*100
            last = self.getsize
            time.sleep(1)
            curr = self.getsize
            down = (curr-last)/1024
            if down > 1024:
                speed = f'{down/1024:6.2f}MB/s'
            else:
                speed = f'{down:6.2f}KB/s'
            print(f'process: {process:6.2f}% | speed: {speed}', end='\r')
            if process >= 100:
                print(f'process: {100.00:6}% | speed:  00.00KB/s', end=' | ')
                break
        tp.shutdown()
        end_time = time.time()
        total_time = end_time-start_time
        average_speed = self.size/total_time/1024/1024
        print(f'total-time: {total_time:.0f}s | average-speed: {average_speed:.2f}MB/s')
        self.flagDone = True


if __name__ == '__main__':
    url='http://31737.org/html/2378.html'
    xpath='/html/body/table[3]/tbody/tr/td/div[2]/div[2]/div/div[2]/li[6]/ul'
    a = geturl(url,xpath)   
    num=0
    for url in a:
        num+=1       
        if num>4:
            break
        if num>2:
            print(url)
            match = re.search(r'/([^/]+)$', url)  # 使用正则表达式匹配最后一个斜杠之后的内容  
            if match:   
                title = match.group(1)  # match.group(1) 返回第一个捕获组的内容，即最后一个斜杠之后的内容 
                print(title)  
            else:  
                print("没有找到匹配项")
            down = downloader(url, 12, title)
            down.main()
            while not down.flagDone:
                time.sleep(1)
        else:
            continue

        # b = geturl(a,'//*[@id="oframeplayer"]/pjsdiv[1]/video')
        # b = geturl(item,'//*[@id="activity-name"]/text()')
        # print(b)
        # 
        # webbrowser.open(a)
        # print(a)
        # a = geturl('https://mp.weixin.qq.com/s/ZdjMycC9XkTOXu9z_58LVg','//*[@id="js_content"]/section/section/section/section/table/tbody/tr/td/a')
            # testText='{}/@href'.format(path)
    # print(testText)
    # content = etree_html.xpath('/html/body/table[3]/tbody/tr/td/div[2]/div[2]/div/div[2]/li[6]/ul/li[3]/a/text()')
    # link = etree_html.xpath('/html/body/table[3]/tbody/tr/td/div[2]/div[2]/div/div[2]/li[6]/ul/li[3]/a/@href')
        
    # for each in content:
    #     replace = each.replace('\n', '').replace('..','http://13777.xyz')
    #     if replace == '\n' or replace == '':
    #         continue
    #     else:
    #         return replace
        # print(content)
    # content = etree_html.xpath(path)
    # browser = webdriver.Chrome()
    # browser.get(content[0])